<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="信息描述">
        <el-tooltip effect="dark" :content="content" placement="top">
          <el-tag type="info">excel模板说明</el-tag>
        </el-tooltip>

        <el-tag>
          <i class="el-icon-download"></i>
          <a :href="defaultExcelTemplate">点击下载模板</a>
        </el-tag>
      </el-form-item>

      <el-form-item label="选择excel">
        <!-- limit 1 限制  :auto-upload 自动上传关闭 ref 相当于id
        document.getElementById("id") name="file" 表示提交的到后端的文档是file属性-->
        <el-upload
          ref="upload"
          :auto-upload="false"
          action="http://127.0.0.1:9110/admin/edu/subject/import"
          :limit="1"
          :on-exceed="handleExceed"
          :on-success="fileUploadSuccess"
          :on-error="fileUploadError"
          name="file"
          accept="application/vnd.ms-excel"
        >
          <el-button slot="trigger" size="small" type="primary"
            >选取文件</el-button
          >
          <el-button
            size="small"
            type="success"
            :disabled="importBtnDisabled"
            style="margin-left: 10px;"
            @click="submitUpload"
          >
            导入
          </el-button>
        </el-upload>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      content: "请先选择模板下载，并进行填充，最后上传",
      // defaultExcelTemplate: process.env.QINIUYUN_PATH + "/课程分类列表模板.xls",
      defaultExcelTemplate:
        // process.env.MINIO_PATH + "/课程分类列表模板_1659250011960.xls",
        "https://education01bucket.oss-cn-hangzhou.aliyuncs.com/课程分类列表模板.xls",
      importBtnDisabled: false, //导入按钮是否禁用
    };
  },
  methods: {
    /**
     * 当选择文件超出数量的时候触发
     */
    handleExceed() {
      this.$message.warning("当前最多上传一个文件！");
    },
    /**
     * 文件上传成功的回调函数
     */
    fileUploadSuccess(resp) {
      this.importBtnDisabled = false;
      this.$message.success(resp.message);
      this.$refs.upload.clearFiles();
    },
    /**
     * 文件上传失败的回调函数
     */
    fileUploadError(resp) {
      this.importBtnDisabled = false;
      this.$message.error(resp.message);
    },
    /**
     * 执行上传文件
     */
    submitUpload() {
      this.importBtnDisabled = true;
      this.$refs.upload.submit(); //手动表单提交 提交到action指定的地址
    },
  },
};
</script>

<style></style>
